<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Trangchu extends MY_Controller { 
	 
	 function __construct()
    {
        parent::__construct();	
		$this->loadtrangchu(""); //ok
		$this->load->model('SanPhan_TrangChu'); 
		
	}
	public function index()
	{  
 
		$this->data['title']=$this->lang->line('TD_TrangChu');//"Trang Chủ"; 
		$this->data['MuaNhieu']=$this->SanPhan_TrangChu->SPMuaNhieu();
		$this->data['XemNhieu']=$this->SanPhan_TrangChu->SPXemNhieu();
		$this->data['Moi']=$this->SanPhan_TrangChu->SPMoi();
		$this->LoadPage('trangchu/Content', $this->data);
 
	} 
	public function SanPham($MaL,$TenL)
	{   
		$this->data['title']=$this->lang->line('TD_SanPham');//"Sản phẩm";
		$this->load->library('pagination'); 
        $this->load->helper('url');
		
		if(empty($MaL)){
		 	$config['total_rows'] = $this->SanPhan_TrangChu->DemDanhSach(); // xác định tổng số record 
			$config['base_url'] = base_url('index.php/'.$this->data['language'].'/Trangchu/SanPham/0/Sản Phẩm'); // xác định trang phân trang 
		}
		else{
			$config['total_rows'] = $this->SanPhan_TrangChu->DemDanhSach_MaL($MaL);
			$config['base_url'] = base_url('index.php/'.$this->data['language'].'/Trangchu/SanPham/'.$MaL.'/'.$TenL); // xác định trang phân trang 
		}
		$config['per_page'] = 6; // xác định số record ở mỗi trang 
		$config['uri_segment'] = 6; // xác định segment chứa page number 
		$this->pagination->initialize($config);  
		if(empty($MaL)) 
			$this->data['SanPham'] = $this->SanPhan_TrangChu->DanhSach($config['per_page'],$this->uri->segment(6));  
		else  
			$this->data['SanPham'] = $this->SanPhan_TrangChu->DanhSach_MaL($MaL,$config['per_page'],$this->uri->segment(6)); 
		$this->data['SP_TieuDe']=$TenL;
		$this->LoadPage('trangchu/SanPham', $this->data); 
	} 
	public function ChiTiet($MaSP)
	{
		$this->load->model('Dathang_model');
		$this->data['title']=$this->lang->line('TD_ChiTiet_SP');//"Chi tiết sản phẩm";	
		$this->data['SanPham']=$this->SanPhan_TrangChu->LaySP($MaSP); 
	    $SanPham=$this->Dathang_model->LuotDatHang($MaSP);  
		$SoLuotXem=array( 
					'MaSP'   		=>$MaSP,
					'SoLuotXem' =>   $SanPham['SoLuotXem']+1
					);	
		$id_TTTT=$this->Dathang_model->UpLuotDatHang($SoLuotXem);  
		
		$this->load->library('pagination'); 
        $this->load->helper('url');  
		$config['total_rows'] = $this->SanPhan_TrangChu->DemNhanXet_MaSP($MaSP); // xác định tổng số record 
		$config['base_url'] = base_url('index.php/'.$this->data['language'].'/Trangchu/ChiTiet' ); // xác định trang phân trang 
	  	$config['per_page'] = 10; // xác định số record ở mỗi trang 
		$config['uri_segment'] = 5; // xác định segment chứa page number 
		$this->pagination->initialize($config); 
		$bien=0;
		if($this->uri->segment(5)==""){$bien=0;}else{$bien=$this->uri->segment(5);}
		$this->data['NhanXet1'] = $this->SanPhan_TrangChu->LayNhanXet_MaSP($MaSP,$config['per_page'],$bien);  
		
		$this->LoadPage('trangchu/ChiTiet', $this->data);	 
	}
	public function LienHe()
	{  
		$this->data['title']=$this->lang->line('TD_LienHe');//"Liên hệ";
		$this->LoadPage('trangchu/LienHe', $this->data); 
	}
	public function GioiThieu()
	{  
		$this->data['title']=$this->lang->line('TD_GioiThieu');//"Giới thiệu";
		$this->LoadPage('trangchu/GioiThieu', $this->data); 
	}
	public function NhanXet($MaSP)
	{  
		$this->data['title']=$this->lang->line('TD_NhanXet');//"Nhận xét";
		$this->load->library('pagination'); 
        $this->load->helper('url');  
		$config['total_rows'] = $this->SanPhan_TrangChu->DemNhanXet_MaSP($MaSP); // xác định tổng số record 
		$config['base_url'] = base_url('index.php/'.$this->data['language'].'/Trangchu/NhanXet/'.$MaSP ); // xác định trang phân trang 
	  	$config['per_page'] = 10; // xác định số record ở mỗi trang 
		$config['uri_segment'] = 5; // xác định segment chứa page number 
		$this->pagination->initialize($config);  
		$bien=0;
		if($this->uri->segment(5)==""){$bien=0;}else{$bien=$this->uri->segment(5);}
		$this->data['NhanXet1'] = $this->SanPhan_TrangChu->LayNhanXet_MaSP($MaSP,$config['per_page'],$bien); 
		$this->data['MaSP']=$MaSP;
		$this->LoadPage('trangchu/NhanXet', $this->data); 
	} 
	public function ThemNhanXet($MaSP)
	{   
		$NX=$this->input->post();
		$TK1= $this->session->userdata("TaiKhoan");  
		if(!empty($TK1)){
			$NhanXet=array( 
					'Ma_TK'         => $TK1['MaTK'],
					'NoiDung'       => $NX['txtNX'],
					'NgayDang'      => date('Y-m-d'),
					'Ma_SP'         => $MaSP
					);  
			$id=$this->SanPhan_TrangChu->ThemNhanXet($NhanXet);
		} else {
			redirect($this->data['language']."/trangchu/index");	
			return;
		}
		redirect($this->data['language']."/trangchu/NhanXet/".$MaSP);	
	} 
	public function TinTuc()
	{  
		$this->data['title']=$this->lang->line('TD_TinTuc');"Tin tức";
		$this->load->model('Tintuc_model');	
		$this->data['tintucq']=$this->Tintuc_model->getresult_index();
		$this->LoadPage('trangchu/TinTuc',$this->data);
	}
	public function chitiettintuc($matt){
		$this->data['title']=$this->lang->line('TD_ChiTiet_T');//"Tin tức chi tiết";
		$this->load->model('Tintuc_model');
		$this->data['tintuc']=$this->Tintuc_model->gettintuc($matt);
		$this->LoadPage('trangchu/ChiTiet_T',$this->data);
	} 
	
	// Tìm sản phảm theo ký tự
	public function TimSP_KT($TuKhoa)
	{   
		$this->data['title']=$this->lang->line('TD_SanPham');//"Sản phẩm";
		$this->load->library('pagination'); 
        $this->load->helper('url');  
		$config['total_rows'] = $this->SanPhan_TrangChu->Dem_TimTheoChu($TuKhoa); // xác định tổng số record 
		$config['base_url'] = base_url('index.php/'.$this->data['language'].'/Trangchu/TimSP_KT/'.$TuKhoa ); // xác định trang phân trang 
	  	$config['per_page'] = 6; // xác định số record ở mỗi trang 
		$config['uri_segment'] = 5; // xác định segment chứa page number 
		$this->pagination->initialize($config);   
		$bien=0;
		if($this->uri->segment(5)==""){$bien=0;}else{$bien=$this->uri->segment(5);}
		$this->data['SanPham'] = $this->SanPhan_TrangChu->TimTheoChu($TuKhoa,$bien,$config['per_page']); 
		$this->data['SP_TieuDe']=""; 
		$this->LoadPage('trangchu/SanPham', $this->data); 
	}
	
	public function TimSP_Ten()
	{   
		$TenSP=$this->input->post();
		$this->session->set_userdata("Ten",$TenSP['txtTen']); // gán vào sesion 
		$Ten = $this->session->userdata("Ten");
		$this->data['title']=$this->lang->line('TD_SanPham');//"Sản phẩm";
		$this->load->library('pagination'); 
        $this->load->helper('url');  
		$config['total_rows'] = $this->SanPhan_TrangChu->Dem_TimTheoTen($Ten); // xác định tổng số record 
		$config['base_url'] = base_url('index.php/'.$this->data['language'].'/Trangchu/TimSP_Ten' ); // xác định trang phân trang 
	  	$config['per_page'] = 6; // xác định số record ở mỗi trang 
		$config['uri_segment'] = 4; // xác định segment chứa page number 
		$this->pagination->initialize($config);   
		$bien=0;
		if($this->uri->segment(4)==""){$bien=0;}else{$bien=$this->uri->segment(4);}
		$this->data['SanPham'] = $this->SanPhan_TrangChu->TimTheoTen($Ten ,$bien,$config['per_page']); 
		$this->LoadPage('trangchu/SanPham', $this->data); 
	}
	
	
	public function TaiKhoan()
	{ 
		$this->data['title']=$this->lang->line('TD_TaiKhoan');//"Tài Khoản";
		$this->data['TK_ThongBao']=""; 
		$this->data['TK_ThongBao_Doi']="";
		$TK= $this->session->userdata("TaiKhoan");  
		$this->data['TK_DatHang']= $this->Events_model->LayDonDH($TK['MaTK']);
		$this->LoadPage('trangchu/TaiKhoan', $this->data); 
	}  
	public function SuaTaiKhoan()
	{ 
		$this->load->model('Events_model');
		$this->data['title']="Tài Khoản"; 
		$this->data['TK_ThongBao']="";//TK_ThongBao_Doi
		$this->data['TK_ThongBao_Doi']="";
		
		$TK=$this->input->post(); 
		$TK1= $this->session->userdata("TaiKhoan");  
		if(!empty($TK1)){
			$TK=array( 
					'MaTK'        => $TK1['MaTK'],
					'HoTen'       => $TK['HoTen'],
					'DiaChi'      => $TK['DiaChi'],
					'SoDienThoai' => $TK['SoDienThoai'],
					'Email'       => $TK['Email']
					);  
			$this->Events_model->SuaTK($TK);  
			$this->data['TK_ThongBao']=$this->lang->line('TDTK_TB_ThanhCong');//"Thay đổi thành công"; 
			$TaiKhoan = $this->Events_model->getaccout($TK1['TenTaiKhoan'],$TK1['MatKhau']); 
			$this->session->set_userdata("TaiKhoan",$TaiKhoan); 
		}
		//redirect("trangchu/TaiKhoan");	
		$this->data['TK_DatHang']= $this->Events_model->LayDonDH($TK['MaTK']);
		$this->LoadPage('trangchu/TaiKhoan', $this->data); 
	} 
	public function DoiMatKhau()
	{ 
		$this->load->model('Events_model');
		$this->data['title']=$this->lang->line('TD_TaiKhoan');//"Tài Khoản"; 
		$this->data['TK_ThongBao']="";
		$this->data['TK_ThongBao_Doi']="";
		$TK=$this->input->post(); 
		$TK1= $this->session->userdata("TaiKhoan"); 
		if(trim($TK['MatKhauC'])<> trim($TK1['MatKhau'])) { 
		 	$this->data['TK_ThongBao_Doi']=$this->lang->line('TDTK_TB_MKCu');//"Mật khẩu củ không đúng";
		} else{
			if(trim($TK['MatKhau'])<>trim($TK['MatKhauXN'])){ 
				$this->data['TK_ThongBao_Doi']=$this->lang->line('TDTK_TB_XacNhan');//"Mật khẩu xác nhận không đúng";
			}else{
			
				if(!empty($TK1)){
					$TK=array( 
							'MaTK'        => $TK1['MaTK'],
							'MatKhau'       => trim($TK['MatKhau']), 
							);  
					$this->Events_model->DoiMK($TK);  
					$this->data['TK_ThongBao_Doi']=$this->lang->line('TDTK_TB_ThanhCong');//"Thay đổi thành công"; 
				}else{
					$this->data['TK_ThongBao_Doi']=$this->lang->line('TDTK_TB_XacNhan');//"Mật khẩu xác nhận không đúng"; //$this->lang->line('DN_KhongTC');
				} 
				$TaiKhoan = $this->Events_model->getaccout($TK1['TenTaiKhoan'],$TK['MatKhau']); 
				$this->session->set_userdata("TaiKhoan",$TaiKhoan); 
			}
		}
		//redirect("trangchu/TaiKhoan");	
		$this->data['TK_DatHang']= $this->Events_model->LayDonDH($TK['MaTK']);
		$this->LoadPage('trangchu/TaiKhoan', $this->data); 
	}
	
	public function Xoa_DH($MaDH){	 // xóa đơn hàng
		$this->load->model('Dathang_model');
		$this->Dathang_model->Xoa_CT($MaDH);
		$this->Dathang_model->Xoa_TT($MaDH);
		$this->Dathang_model->Xoa($MaDH);
		
		redirect($this->data['language']."/trangchu/TaiKhoan");	
	}
	
	public function ChiTiet_DH($MaDH){	 // chi tiết đơn hàng 
		$this->load->model('Dathang_model'); 
		$this->data['title']=$this->lang->line('TD_ChiTiet_DH');//"Chi Tiết Đơn Hàng"; 
		$this->data['TK_DDH']= $this->Dathang_model->LayDDH_MaDDH($MaDH);   // Đơn đặt hàng
		$this->data['TK_TTTT']= $this->Dathang_model->LayTTTT_DDH_MaDDH($MaDH);  // thông tin hanh toán
		$this->data['TK_CT_DDH']= $this->Dathang_model->LayCT_DDH_MaDDH($MaDH);// chi tiết đơn hàng 
		$this->LoadPage('trangchu/ChiTiet_DH', $this->data);	
	}
	
	public function DangNhap(){	//from đăng nhập	
		$this->data['lbThongBao']=  "" ; 
		$this->data['title']=$this->lang->line('TD_DangNhap');//"Đăng Nhập";
		$this->load->view('DangNhap',$this->data);
		 
	}
	 
	public function check(){//kiem tra from dangnhap
		$this->load->model('Events_model');
		$data = $this->input->post();
		$TaiKhoan = $this->Events_model->getaccout($data['txtTK'],$data['txtMK']); 
		if(!empty($TaiKhoan))
		{
			$this->session->set_userdata("TaiKhoan",$TaiKhoan); 
			if($TaiKhoan['TrangThai']<>0){ 								//Khóa khi bằng  TrangThai =0 
				if($TaiKhoan['TenTaiKhoan']<>""){
				  	if($TaiKhoan['Quyen']==1){							// 1 admin 
				  		redirect("quanlysanpham/index"); 
					}else{												// 2 người dùng 
						 redirect($this->data['language']."/trangchu/index");
					} 
				}else{					
				 	$this->data['lbThongBao']=  $this->lang->line('DN_KhongTC');	// không thành công
					$this->load->view('DangNhap',$this->data);
				} 
			}else{
				$this->data['lbThongBao']= $this->lang->line('DN_Khoa') ;			// tài khoản khóa
				$this->load->view('DangNhap',$this->data);
			} 
		}else{
			$this->data['lbThongBao']= $this->lang->line('DN_KhongTC') ;			// không thành công
			$this->load->view('DangNhap',$this->data); 
		}  
	} 
	 
	public function DangXuat(){//thoat khoi he thong tro ve trang chu
		$this->session->sess_destroy();
		redirect($this->data['language']."/trangchu/index");	
	}
	public function DangKy()
	{ 
	$this->load->helper(array('url','language','form'));
		$this->load->library('form_validation');
		$this->data['title']=$this->lang->line('TD_DangKy');//"Đăng Ký";
		$this->data["TB_DangKy"]="";
		$this->load->view('DangKy',$this->data);   
	}
	public function TimKiem()
	{
		 $this->data['title']="Sản phẩm";
		 $this->load->helper('url'); // cấu hình phân trang //$data2["number"] = $this->giaodien_md->number($id); //paging 
		 $this->load->library('pagination');
		 $this->data['query']= $this->input->get();
		 $config['base_url'] = base_url('index.php/'.$this->data['language'].'/Trangchu/TimKiem/?ma_dm='.$this->data['query']['ma_dm'].'&key='.$this->data['query']['key']);
		 $config['total_rows'] = $this->SanPhan_TrangChu->DemDanhSach_TK($this->data['query']); // xác định tổng số record 
		 $config['per_page'] =6;
		 $config['num_links'] =4;
		 $config['use_page_numbers'] = TRUE;
		 $config['page_query_string']=TRUE;
		 $config['query_string_segment']='page';
		 ((int)$this->input->get("page"))?$currentPage=(int)$this->input->get("page"):$currentPage=1;
		 $currentPage = $config['per_page']*($currentPage - 1);
		
		$this->pagination->initialize($config);
		$this->data['SanPham'] = $this->SanPhan_TrangChu->DanhSach_TK($this->data['query'],$currentPage, $config['per_page']);
		$this->LoadPage('trangchu/TimKiem_SP', $this->data); 
	}
	
	public function ThemTaiKhoan()
	{ 
		
		$this->data['title']=$this->lang->line('TD_DangKy');//"Đăng Ký";
		$this->load->model('Dathang_model');
		$DK_TaiKhoan =	$this->input->post();
		$this->data["TB_DangKy"]=""; 
		$TaiKhoan=$this->Dathang_model->KiemTraTK(trim($DK_TaiKhoan['txtTK'])); 
		$this->load->helper(array('url','language','form'));
		$this->load->library('form_validation'); 
		if($this->form_validation->run('frmDangKy')==false ){
				redirect($this->data['language']."/trangchu/DangKy");	
			}
		if(count($TaiKhoan)>0){
			$this->data["TB_DangKy"]=$this->lang->line('DK_TB2');//"Đăng ký không thành công."; 
			$this->load->view('DangKy',$this->data);  
			return;} 
		if(!empty($TaiKhoan)){
			 
				$this->data["TB_DangKy"]=$this->lang->line('DK_TB1');//"Tài khoản đã tồn tại";
				$this->load->view('DangKy',$this->data); 
				return; 
		}else{
			$TK=array(  
					'TenTaiKhoan' =>$DK_TaiKhoan['txtTK'],
					'MatKhau'     =>$DK_TaiKhoan['txtMK'],
					'HoTen'       =>$DK_TaiKhoan['txtHT'],
					'DiaChi'      =>$DK_TaiKhoan['txtDC'],
					'SoDienThoai' =>$DK_TaiKhoan['txtSDT'],
					'Email'       =>$DK_TaiKhoan['txtM'], 
					'TrangThai'   =>1,
					'Quyen'       =>2
					);   
			$id=$this->Dathang_model->ThemTaiKhoan($TK); 
			if($id>0){
				$url= base_url('index.php/'.$this->data['language'].'/Trangchu/fromXacNhan/'.$id.'/1/33');
				$this->data["TB_DangKy"]=$this->lang->line('DK_TB4');//"Đăng ký thành công.";
				$message=$this->lang->line('DK_Mail_1'). "\r\n" .
						 $this->lang->line('DK_Mail_2'). "\r\n" .
						 $this->lang->line('DK_Mail_3').$DK_TaiKhoan['txtHT'] . "\r\n" .
						 $this->lang->line('DK_Mail_4'). "\r\n" .
						 ''.$url.''."\r\n" .
						 $this->lang->line('DK_Mail_5'). "\r\n" ; 
				//$this->send_email('chancntt09@gmail.com', 'chancntt09@gmail.com', 'subject', 'message body');
				$this->send_email($message,$DK_TaiKhoan['txtM'],$this->lang->line('DK_Mail_6')); //"Xác Nhận Đăng Ký"
				$this->data["TB_DangKy"]=$this->lang->line('DK_TB4');//"Đăng ký thành công."; 
				$this->load->view('DangKy',$this->data);  
				return;
			}else{
				 
				$this->data["TB_DangKy"]=$this->lang->line('DK_TB2');//"Đăng ký không thành công."; 
				$this->load->view('DangKy',$this->data);  
				return;
			} 
		}
	}
	public function fromXacNhan($id,$Loai,$MaXN)
	{ 
		$this->data['title']=$this->lang->line('TD_XacNhan');//"Xác Nhận";
		$this->load->model('Dathang_model');
		$this->data["TB_XacNhan"]=""; 
		$this->data['DK_Loai']=$Loai;
		if($Loai){
			$TK=array(  
					
					'MaTK'        =>$id, 
					'TrangThai'   =>2, 
					);   
			$id=$this->Dathang_model->UpTaiKhoan($TK);
			$this->data["TB_XacNhan"]= $this->lang->line('DK_TB3');//"Bạn đã hoàn thành đăng ký.";
		}
		if($Loai){
			$TK=array(   
					'MaTK'        =>$id, 
					'TrangThai'   =>2, 
					);   
			$id=$this->Dathang_model->UpTaiKhoan($TK);
			$this->data["TB_XacNhan"]=$this->lang->line('QMK_TB3');// "Bạn đã hoàn thành viêc xác nhận.";
		}
		$this->load->view('XacNhan',$this->data);   
	}
	public function QuenMatKhau()
	{ 	
		$this->data['title']=$this->lang->line('TD_QuenMK');//"Quên Mật Khẩu"; 
		$this->data['QMK_ThongBao']="";
		$this->load->view('QuenMatKhau',$this->data);   
	}
	public function CheckQuenMatKhau()
	{ 
		$this->data['title']=$this->lang->line('TD_QuenMK');//"Quên Mật Khẩu"; 
		$this->data['QMK_ThongBao']="";
		$this->load->model('Dathang_model');
		$TK_QuenTK = $this->input->post(); 
		$TaiKhoan=$this->Dathang_model->KiemTraTK(trim($TK_QuenTK['txtTK'])); 
		if(trim($TK_QuenTK['txtM']) <> trim($TaiKhoan['Email']))
		{
			$this->data['QMK_ThongBao']=$this->lang->line('QMK_MailKhongDung');//"Mail không đúng.";
		}else{
			$MaXacNhan =random_string('numeric', 8);
			$Re_TK=array(   
					'MaTK'        =>$TaiKhoan['MaTK'], 
					'MaXacNhan'   => $MaXacNhan , 
					'NgayHetHan'   => $this->CongNgay()
					);   
			$id=$this->Dathang_model->ThemTaiKhoan_Re($Re_TK);  
			if($id>0){
				$url= base_url('index.php/'.$this->data['language'].'/Trangchu/fromXacNhan/'.$id.'/2/'.$MaXacNhan );
				$message=	  $this->lang->line('QMK_Mail_1')."\r\n" .
							  $this->lang->line('QMK_Mail_2')."\r\n" .
							  $this->lang->line('QMK_Mail_3').$TaiKhoan['TenTaiKhoan'] . "\r\n" .
							  $this->lang->line('QMK_Mail_4').$TaiKhoan['MatKhau'] . "\r\n" .
							  'Mail: '.$TaiKhoan['Email'] . "\r\n" .
							  $this->lang->line('QMK_Mail_5')."\r\n" . 
							  ''.$url.''."\r\n" .
							  $this->lang->line('QMK_Mail_6')."\r\n" ;   
				$this->send_email($message,$TK_QuenTK['txtM'],$this->lang->line('QMK_Mail_7')); //"Xác Nhận Đăng Ký"
				$this->data['QMK_ThongBao']=$this->lang->line('QMK_TB2');//"Chúng tôi đã gủi mail cho bạn.";
				$TK=array(   
					'MaTK'        =>$id, 
					'TrangThai'   =>1, 
					);   
			$id=$this->Dathang_model->UpTaiKhoan($TK);
			}else{
				$this->data['QMK_ThongBao']=$this->lang->line('QMK_TB3');//"Không thành công vui lòng thử lại.";
			}
		}
		$this->load->view('QuenMatKhau',$this->data);   
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */